package com.hisan.freeride.home.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hisan.freeride.R;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.ConvertUtils;
import com.hisan.freeride.home.model.TList;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页行程列表适配器
 * Created by yu on 2017/1/17.
 */

public class HomeAdapter extends RecyclerView.Adapter{
    private  List<TList.DataBean> mList;

    private ButtonClickListener mButtonClickListener;

    public HomeAdapter(Context context,List<TList.DataBean> list) {
            mList = list;

    }
    public void setNewData(List<TList.DataBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

     public void setButtonClickListener(ButtonClickListener listener) {
        mButtonClickListener = listener;
    }

    /**
     * 按钮的点击监听
     */
    public interface ButtonClickListener {
        void onClick(int p, TList.DataBean data);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HomeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_list_item,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TList.DataBean homeModel=mList.get(position);
        final HomeViewHolder viewHolder = (HomeViewHolder) holder;
        viewHolder.simpleDraweeView.setOnClickListener(v -> {
            if (mButtonClickListener != null){
                mButtonClickListener.onClick(0,homeModel);
            }
        });
        viewHolder.status.setOnClickListener(v -> {
            if (mButtonClickListener != null){
                mButtonClickListener.onClick(1,homeModel);
            }
        });
        if (!CollectionUtils.isNullOrEmpty(homeModel.getDriver_avatar())){
            Uri uri = Uri.parse(homeModel.getDriver_avatar());
            viewHolder.simpleDraweeView.setImageURI(uri);
        }else{
            viewHolder.simpleDraweeView.setImageResource(R.mipmap.avator);
        }
        viewHolder.name.setText(homeModel.getDriver_name());
        //累计助人
        viewHolder.number.setText(+homeModel.getHelp_count()+"");
        viewHolder.plate.setText(homeModel.getCar_no());
        if (!CollectionUtils.isNullOrEmpty(homeModel.getDeparture_time()))
        viewHolder.time.setText(homeModel.getDeparture_time()+"");
        viewHolder.total.setText("剩余" + homeModel.getSurplus() + "人"+","+"可乘"+homeModel.getPeople()+"人");
        viewHolder.site.setText("出发地: "+homeModel.getInitially());
        viewHolder.way.setText("途径地: "+homeModel.getWay());
        viewHolder.address.setText("目的地: "+homeModel.getDestination());
        String time=homeModel.getDeparture_time()+":00";
        if (homeModel.getIs_expired()==1&&getTime(time)>0){
            viewHolder.expired.setVisibility(View.VISIBLE);
            viewHolder.status.setVisibility(View.GONE);
        }else {
            viewHolder.expired.setVisibility(View.INVISIBLE);
            viewHolder.status.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }
    // 将字符串转为时间戳
    public  long getTime(String stime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        long st= ConvertUtils.timeDifference(stime,str);
        return st;
    }
}
